package com.lzw.bluetoothpractice.Adapters;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.lzw.bluetoothpractice.Activitys.HealthyMessagesActivity;
import com.lzw.bluetoothpractice.Activitys.SendMessageActivity;
import com.lzw.bluetoothpractice.AppConfig;
import com.lzw.bluetoothpractice.Models.SendMessage;
import com.lzw.bluetoothpractice.Models._User;
import com.lzw.bluetoothpractice.R;
import com.lzw.bluetoothpractice.bean.RecommendDoctorBean;
import com.lzw.bluetoothpractice.bean.SystemUserBean;
import com.lzw.bluetoothpractice.bean.SystemUserListBean;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.newim.BmobIM;
import cn.bmob.newim.bean.BmobIMConversation;
import cn.bmob.newim.bean.BmobIMUserInfo;
import cn.bmob.newim.listener.ConnectListener;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;


public class UserReadMessageListAdapter extends RecyclerView.Adapter<UserReadMessageListAdapter.ViewHolder>  {

    private List<SystemUserListBean.User> datas = new ArrayList<>();
    View itemView;
    Context mcontext;
    public void updateData(List<SystemUserListBean.User> data) {
        if (this.datas == null) {
            this.datas = new ArrayList<>();
        }
        this.datas.clear();
        this.datas.addAll(data);
        notifyDataSetChanged();
    }

    public UserReadMessageListAdapter(Context mcontext) {
        this.mcontext = mcontext;

    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        itemView= LayoutInflater.from(mcontext).inflate(R.layout.user_read_message,parent,false);

        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        SystemUserListBean.User dataBean=datas.get(position);
        Glide.with(mcontext).load(AppConfig.BASE_URL+dataBean.getAvatar()).into(holder.iv_head);
        holder.tv_chat_name.setText(dataBean.getUserName());
        if(!dataBean.getRemark().equals("0")){
            holder.no_read_messages.setVisibility(View.VISIBLE);
            holder.no_read_messages.setText(dataBean.getRemark());
        }
        holder.last_chat_time.setVisibility(View.GONE);




        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {



                Intent intent = new Intent();
                intent.setClass(mcontext, SendMessageActivity.class);
                Bundle bundle = new Bundle();

                bundle.putString("Name", dataBean.getNickName());

                bundle.putString("Head", dataBean.getAvatar());
                bundle.putString("ID", dataBean.getUserId()+"");
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
                if (bundle != null) {
                    intent.putExtras(bundle);
                }
                mcontext.startActivity(intent);



            }
        });

    }

    @Override
    public int getItemCount() {
        return datas.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        View view;

        TextView tv_chat_name;
        TextView tv_position;
        TextView tv_last_read;
        ImageView iv_head;
        TextView last_chat_time;
        TextView no_read_messages;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            view=itemView;
            tv_chat_name=view.findViewById(R.id.tv_chatname);
            tv_position=view.findViewById(R.id.tv_position);
            last_chat_time=view.findViewById(R.id.last_chat_time);
            tv_last_read=view.findViewById(R.id.last_chat);
            iv_head=view.findViewById(R.id.iv_head);
            no_read_messages=view.findViewById(R.id.no_read_messages);
        }
    }
}
